package aspirecn.project3.mapper;

import aspirecn.project3.model.GoodsDetail;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface GoodsDetailMapper {

    @Select("SELECT * FROM good_detail WHERE GOOD_ID = #{id}")
    List<GoodsDetail> getGoodsById(long id);

    @Insert("Insert into good_detail(TITLE, SUBTITLE, DETAIL, POINT, FIRST_PICTURE_ADDRESS, FIRST_DIRECTORY, SECOND_DIRECTORY, CLINK_COUNT, CLINK_TIME, LISTING_TIME, UPDATE_TIME, GOODS_STATES) " +
            "values(#{TITLE}, #{SUBTITLE}, #{DETAIL}, #{POINT}, #{FirstPictureAddress}, #{FirstDirectory}, #{SecondDirectory}, #{ClinkCount}, #{ClinkTime}, #{ListingTime}, #{UpdateTime}, #{GoodsStates})")
    int addGoods(GoodsDetail goodsDetail);

    @Select("SELECT * FROM good_detail WHERE TITLE LIKE CONCAT('%',#{KeyWord},'%')   LIMIT #{page}, #{limit}")
    List<GoodsDetail> getGoodsByPage(@Param("KeyWord") String KeyWord, @Param("page")int page, @Param("limit")int limit);

    @Update("UPDATE good_detail SET TITLE = #{TITLE}, SUBTITLE= #{SUBTITLE}, DETAIL= #{DETAIL}, POINT= #{POINT}, FIRST_PICTURE_ADDRESS= #{FirstPictureAddress}, FIRST_DIRECTORY= #{FirstDirectory}, SECOND_DIRECTORY= #{SecondDirectory}, " +
            "CLINK_COUNT= #{ClinkCount}, CLINK_TIME= #{ClinkTime}, LISTING_TIME= #{ListingTime}, UPDATE_TIME= #{UpdateTime}, GOODS_STATES= #{GoodsStates} WHERE GOOD_ID = #{GoodId}")
    int modifyGood(GoodsDetail goodsDetail);

    @Delete("DELETE FROM good_detail WHERE GOOD_ID = #{id}")
    int deleteGoods(long id);

//    @Insert("<script>"  +
//            "insert into user(id, name, age) VALUES " +
//            "<foreach collection='list' item='item' index='index' separator=','> " +
//            "(#{item.id},#{item.name},#{item.age}) " +
//            "</foreach>" +
//            "</script>")
//    int batchdelete(@Param("list")List<GoodsDetail> list); //批量添加用户

    @Select("SELECT * FROM good_detail WHERE TITLE LIKE CONCAT('%',#{KeyWord},'%') ORDER BY ${col} LIMIT #{page}, #{limit}")
    List<GoodsDetail> getOrderByColum(@Param("page") int page, @Param("limit") int limit, @Param("KeyWord") String KeyWord, @Param("col") String col);

    @Select("SELECT * FROM good_detail WHERE TITLE LIKE CONCAT('%',#{KeyWord},'%') ORDER BY ${col} DESC LIMIT #{page}, #{limit}")
    List<GoodsDetail> getOrderDescByColum(@Param("page") int page, @Param("limit") int limit, @Param("KeyWord") String KeyWord, @Param("col") String col);

    @Update("UPDATE good_detail SET GOODS_SORT = '0' WHERE good_id = #{id}")
    int modifySORT(long id);

    @Update("UPDATE good_detail SET GOODS_SORT = '0' WHERE good_id = #{id}")
    int setTop(long id);

    @Update("UPDATE good_detail SET GOODS_SORT = '1' WHERE good_id = #{id}")
    int cancelTop(long id);

    @Select("Select GOODS_SORT FROM good_detail  WHERE good_id = #{id}")
    List<String> selectTOP(long id);

    @Select("Select * FROM good_detail WHERE GOODS_SORT = '0'")
    List<GoodsDetail> getTopGoods();

    @Select("Select * FROM good_detail WHERE POINT < #{max} and POINT > #{min}")
    List<GoodsDetail> getClosePoint(@Param("min") double min, @Param("max") double max);

    //点击量最高的商品
    @Select("Select * FROM good_detail ORDER BY CLINK_COUNT DESC limit #{number}")
    List<GoodsDetail> getByPv(int number);

    //随机取数据
    @Select("Select * FROM good_detail ORDER BY RAND() limit #{number}")
    List<GoodsDetail> getByRandom(int number);

    //拉取四个goods作为推荐
    @Select("Select * FROM good_detail limit 4")
    List<GoodsDetail> getFour();

    //根据id插入point
    @Update("UPDATE good_detail SET CLINK_COUNT = CLINK_COUNT + #{number}  WHERE good_id = 1 ")
    int addPoint(@Param("id") Long id, @Param("number") int point);
}
